/**
 * Copyright (c) 2011 Nokia Corporation.
 */

#include <QApplication>
#include <math.h>
#include <QDebug>
#include "mygamewindow.h"
#include "GLES11SpriteBatch.h"



/*!
  Constructor.
  Just zero everything. The initialization is done later at onCreate
*/
MyGameWindow::MyGameWindow(QWidget *parent /* = 0 */)
    : GE::GameWindow(parent),
      spriteBatch(0)
{

}


/*!
  Destructor.
*/
MyGameWindow::~MyGameWindow()
{
    // Destroying already donw at onDestroy
}


/*!
  The size of our window has been changed. Set new projectionmatrix..
*/
void MyGameWindow::onSizeChanged(int w, int h) {
    GLfloat mproj[16] = {
        1.0f, 0, 0, 0,
        0, (float)w / (float)h, 0, 0,
        0, 0, 0, 0,
        0, 0, 0, 1,
    };
    memcpy( matrixProjection, mproj, sizeof( GLfloat ) * 16 );
    currentWidth = w;
    currentHeight = h;
    if (spriteBatch) spriteBatch->setTargetSize( w, h );
}



/*!
    From GameWindow
    \see GameWindow.cpp
*/
void MyGameWindow::onCreate()
{
    spriteBatch = new GLES11SpriteBatch(width(), height());
    glViewport(0,0, width(), height());

    texture = glhelpLoadTexture( ":/graphics/sprite1.png");
}


/*!
    From GameWindow
    \see GameWindow.cpp
*/
void MyGameWindow::onDestroy()
{
    if (spriteBatch) {
        delete spriteBatch;
        spriteBatch = 0;
    }

    glDeleteTextures(1, &texture );

}


/*!
    From GameWindow
    \see GameWindow.cpp
*/
void MyGameWindow::onUpdate(const float frameDelta)
{
    tester.run( frameDelta );
}


/*!
  From GameWindow. Per frame rendering. You can use OpenGL ES 2.0 functionality
  freely here.
  \see GameWindow.cpp
*/
void MyGameWindow::onRender()
{
        // Clear the screen and depthbuffer
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


        // NOTE: You cannot manuallu change GLES2 state with direct commands between
        // spritebatch begin/end without breaking it.

        // SpriteBatch is still on developement stage and doesn�'t support any
        // other projection than custom: You must provide 4x4 projectionmatrix for
        // it manually.
    //spriteBatch->begin( SpriteBatch::eCUSTOMPROJECTION, matrixProjection );
    spriteBatch->begin(SpriteBatch::eALPHA);
    tester.draw( spriteBatch, texture );            // draw the particles


    spriteBatch->end();
}



